/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.http.server;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author hstel-hxg
 */
public class RequestLogger {

    static File logFileDir = new File("./log/request_uri/");
    static File logFile;
    //long logFileId = 0;
    static SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
    static transient long lastLogFileTime = System.currentTimeMillis();
    static transient FileWriter fw = null;
    

    private static FileWriter getLogFileWriter() throws IOException {
        if (!logFileDir.exists()) {
            logFileDir.mkdirs();
        }
        if (fw == null) {
            fw = new FileWriter(new File(logFileDir, "request_"+sdf.format(new Date(System.currentTimeMillis())) + ".log"));
        } else if (System.currentTimeMillis() > lastLogFileTime + 300000) {
            lastLogFileTime = System.currentTimeMillis();
            fw = new FileWriter(new File(logFileDir, sdf.format(new Date(System.currentTimeMillis())) + ".log"));
        }
        return fw;
    }

    final static void log(String s) throws IOException {
        FileWriter wr = getLogFileWriter();
        wr.write(s);
        wr.flush();
    }    

}
